<?php 
class ContactController extends Zend_Controller_Action
{
	public function init()
	{
		Application_Helper_Authenticate::checkLogin($this);
	}

	public function indexAction()
	{
		$Contacts = new Application_Model_DbTable_Contact();
		$this->view->contacts = $Contacts->fetchAll();

	}

	public function addAction()
	{

		$form = new Application_Form_Contact();
		$this->view->form = $form;
		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData))
			{
				$email = $form->getValue('email');
				//$confirmed = $form->getValue('confirmed');
				//$state = $form->getValue('state');
				$listId = $form->getValue('list_id');
				$contact = new Application_Model_DbTable_Contact();

				$contact->addContact($email, 1, 'active', $listId);


				$this->_helper->redirector('index');
			}
			else
			{
				$form->populate($formData);
			}
		}
	}

	public function editAction()
	{
		$form = new Application_Form_Contact();
		$form->submit->setLabel('save');
		$this->view->form = $form;

		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData))
			{
				$id = $form->getValue('id');
				$email = $form->getValue('email');
				$confirmed = $form->getValue('confirmed');
				$state = $form->getValue('state');
				$listId = $form->getValue('list_id');
				
				$contact = new Application_Model_DbTable_Contact();
				$contact->updateContact($id, $email, $confirmed, $state, $listId);

				$this->_helper->redirector('index');
			}
			else
			{
				$form->populate($formData);
			}
		}
		else
		{
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$contact = new Application_Model_DbTable_Contact();
				$form->populate($contact->getContact($id));
			}
		}
	}

	public function deleteAction()
	{
		if ($this->getRequest()->isPost()) {
			$del = $this->getRequest()->getPost('del');
			if ($del == 'Yes') {
				$id = $this->getRequest()->getPost('id');
				
				$contact = new Application_Model_DbTable_Contact();
				$contact->deleteContact($id);
			}
			$this->_helper->redirector('index');
		}
		else 
		{
			$id = $this->_getParam('id', 0);
			
			$contact = new Application_Model_DbTable_Contact();
			$this->view->contact = $contact->getContact($id);
		}
	}
}